package com.songshuai.webapi.baseapi.controller;

import com.songshuai.webapi.baseapi.dto.request.AddMasterNumberReqDto;
import com.songshuai.webapi.baseapi.dto.request.UpdateMasterNumberReqDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.pojo.feign.request.MasterNumberListParamReqDto;
import com.songshuai.webapi.baseapi.pojo.feign.request.MasterNumberParamReqDto;
import com.songshuai.webapi.baseapi.service.SchoolMasterNumberService;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author:wbl
 * @Date: 2019/1/3
 */
@Api("线下校长端自增记录")
@Slf4j
@RestController
@RequestMapping("/api/masternumber/")
public class SchoolMasterNumberController {

    @Autowired
    private SchoolMasterNumberService schoolMasterNumberService;

    @ApiOperation(value = "获取自增记录数据")
    @PostMapping("queryNumberValueByParam")
    public QueryResponse<String> queryNumberValueByParam(@Valid @RequestBody MasterNumberParamReqDto parmDto, BindingResult result){
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        return QueryResponse.setResData(schoolMasterNumberService.queryNumberValueByParam(parmDto));
    }

    @ApiOperation(value = "新增自增记录")
    @RequestMapping("addMasterNumber")
    public QueryResponse addMasterNumber(@RequestBody AddMasterNumberReqDto parmDto){
        try{
            boolean isSucc = schoolMasterNumberService.addMasterNumber(parmDto);
            if(isSucc) {
                return new QueryResponse();
            }
        } catch (Exception e) {
            log.error("api/masternumber/addMasterNumber", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }

    @ApiOperation(value = "修改自增记录")
    @RequestMapping("updateMasterNumber")
    public QueryResponse updateMasterNumber(@RequestBody UpdateMasterNumberReqDto parmDto){
        try{
            boolean isSucc = schoolMasterNumberService.updateMasterNumber(parmDto);
            if(isSucc) {
                return new QueryResponse();
            }
        } catch (Exception e) {
            log.error("api/masternumber/updateMasterNumber", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
        return new QueryResponse(ResultStatus.ERROR_UPDATE_FAILED);
    }

    @ApiOperation(value = "获取自增记录数据批量")
    @PostMapping("queryNumberValueListByParam")
    public QueryResponse<List<String>> queryNumberValueListByParam(@Valid @RequestBody MasterNumberListParamReqDto parmDto, BindingResult result){
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        return QueryResponse.setResData(schoolMasterNumberService.queryNumberValueListByParam(parmDto));
    }


}
